//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "server\fn_serverExecuteHostage.sqf"
#include <tmf_constants.h>
#include <tmf_macros.h>

TRACE_1("Start",_this);
TMFPROFILERSTART;

private ["_object","_action","_parameters","_player","_action_id","_freed_msg","_die_msg","_shared_free","_shared_alive","_free_code","_die_code","_evt_idx"];
DEFAULT_PARAM(_object,TMF_EXECUTE_ACTION_OBJECT,objNull);
DEFAULT_PARAM(_caller,TMF_EXECUTE_ACTION_CALLER,objNull);
DEFAULT_PARAM(_act_id,TMF_EXECUTE_ACTION_ID,-1);
DEFAULT_PARAM(_arguments,TMF_EXECUTE_ACTION_ARGS,[]);
DEFAULT_PARAM(_action,TMF_EXECUTE_ACTION_ACTION,[]);
_action_id = _arguments select TMF_EXECUTE_ACTION_PARAM_ACTID;

if (!isnull _object) then {
	if (_action_id > 0) then {
		_instance_id = [_object] call FFUNC(getInstanceID);
		if (count _action  <= 0) then {
			_action = [_object,_action_id] call FFUNC(actionGet);
		};
		TRACE_1("Action",_action);
		_parameters = [_action] call FFUNC(actionGetParams);

		_freed_msg = [_parameters, "FREED_MSG", ""] call CFUNC(hashGet);
		_die_msg = [_parameters, "DIE_MSG", ""] call CFUNC(hashGet);
		_shared_free = [_parameters, "SHARED_FREE_VAR", ""] call CFUNC(hashGet);
		_shared_alive = [_parameters, "SHARED_ALIVE_VAR", ""] call CFUNC(hashGet);
		_free_code = [_parameters, "FREE_CODE", ""] call CFUNC(hashGet);
		_die_code = [_parameters, "DIE_CODE", ""] call CFUNC(hashGet);
		
		_hostage_pos = getPosASL _object;
		_object setCaptive false;
		_object enableAI "AUTOTARGET";
		_object enableAI "MOVE";
		_object allowDamage false;
		
		if (typename _freed_msg == typename "") then {_freed_msg = [_freed_msg];};
		if (typename _die_msg == typename "") then {_die_msg = [_die_msg];};

		if (!isNil "_freed_msg") then {
			if (count _freed_msg > 0) then {[QGVAR(all_message),[_freed_msg,"all",objNull,"hq"],THIS_FILE_] call NFUNC(callEvent);};
		};
		if (!isNil "_shared_free") then {
			if (_shared_free != "") then {
				[_instance_id,_shared_free,true] call FFUNC(setMissionJIPVar);
				[QGVAR(all_var), [_instance_id,_shared_free,true],THIS_FILE_] call NFUNC(callEvent);
			};
		};
		if (!isNil "_shared_alive") then {
			if (_shared_alive != "") then {
				[_instance_id,_shared_alive,alive _object] call FFUNC(setMissionJIPVar);
				[QGVAR(all_var), [_instance_id,_shared_alive,alive _object],THIS_FILE_] call NFUNC(callEvent);
			};
		};

		_evt_idx = [_object,_action,"HOSTAGE_KILLED_EVENT",-1] call FFUNC(actionGetLocal);
		if (!isNil "_evt_idx" && _evt_idx >= 0) then {
			_object removeEventHandler ["killed", _evt_idx];
			TRACE_2("Removed Event",_evt_idx,"HOSTAGE_KILLED_EVENT");
			[_object,_action,"HOSTAGE_KILLED_EVENT",-1] call FFUNC(actionSetLocal);
		};

		if (_free_code != "") then {
			[QGVAR(all_execute),[_free_code,_object],THIS_FILE_] call NFUNC(callEvent);
		};
		
		if (alive _object) then {
			sleep 1;
			if (!isnull _caller) then {
				if ((_caller distance _object) < 10) then {
					_object setPos (getPos _caller);
				} else {
					_object setPosASL _hostage_pos;
				};
			} else {
				_object setPosASL _hostage_pos;
			};
			sleep 5;
			_object allowDamage true;
		};
	};
};	
TRACE("End");
TMFPROFILERSTOP;